package com.itheima.health.service.impl;

import com.itheima.health.dao.MemberDao;
import com.itheima.health.dao.OrderDao;
import com.itheima.health.pojo.Order;
import com.itheima.health.service.ReportService;
import com.itheima.health.util.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 数据报表service
 */
@Service
@Slf4j
public class ReportServiceImpl implements ReportService {
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;

    //运营数据统计
    @Transactional
    @Override
    public Map<String, Object> getBusinessReportData() {
        log.info("运营数据统计 业务层 开始执行");
        //构建一个用于存储前端需求数据的map集合
        Map<String, Object> reportData = new HashMap<>();
        //存入报告日期
        reportData.put("reportDate", DateUtils.getTodayDate());
        //获取有关会员数据的信息, 并存入集合中
        Integer totalMember = memberDao.selectCountTotalMember();//总会员数
        reportData.put("totalMember", totalMember);
        Integer todayNewMember = memberDao.selectCountNewMemberByDate(DateUtils.getTodayDate());//今日新增
        reportData.put("todayNewMember", todayNewMember);
        Integer thisWeekNewMember = memberDao.selectCountNewMemberByDate(DateUtils.getFirstDayOfWeek());//本周新增
        reportData.put("thisWeekNewMember", thisWeekNewMember);
        Integer thisMonthNewMember = memberDao.selectCountNewMemberByDate(DateUtils.getFirstDayOfMonth());//本月新增
        reportData.put("thisMonthNewMember", thisMonthNewMember);
        //获取有关预约到诊数据的信息, 并存入集合中
        Integer todayOrderNumber = orderDao.selectCountOrderNumberByDate(DateUtils.getTodayDate(), null);//今日预约
        reportData.put("todayOrderNumber", todayOrderNumber);
        Integer thisWeekOrderNumber = orderDao.selectCountOrderNumberByDate(DateUtils.getFirstDayOfWeek(), null);//本周预约
        reportData.put("thisWeekOrderNumber", thisWeekOrderNumber);
        Integer thisMonthOrderNumber = orderDao.selectCountOrderNumberByDate(DateUtils.getFirstDayOfMonth(), null);//本月预约
        reportData.put("thisMonthOrderNumber", thisMonthOrderNumber);
        Integer todayVisitsNumber = orderDao.selectCountOrderNumberByDate(DateUtils.getTodayDate(), "已到诊");//今日到诊
        reportData.put("todayVisitsNumber", todayVisitsNumber);
        Integer thisWeekVisitsNumber = orderDao.selectCountOrderNumberByDate(DateUtils.getFirstDayOfWeek(), "已到诊");//本月到诊
        reportData.put("thisWeekVisitsNumber", thisWeekVisitsNumber);
        Integer thisMonthVisitsNumber = orderDao.selectCountOrderNumberByDate(DateUtils.getFirstDayOfMonth(), "已到诊");//本月到诊
        reportData.put("thisMonthVisitsNumber", thisMonthVisitsNumber);
        //将获取到的热门套餐数据封装到一个map中, 并存入集合
        List<Map<String, Object>> hotSetmeal = orderDao.selectListHotSetmeal();
        reportData.put("hotSetmeal", hotSetmeal);
        //返回结果
        return reportData;
    }
}
